package com.yxzc.chapter16.homework

/**
 *
 * @author wu
 * @create 2021-12-20-16:48 
 */
object WorkCount {
  def main(args: Array[String]): Unit = {
    //1 第一步扁平化
    var lines = List("atguiug han hello", "atguigu han aaa ccc bbb aaa ddd uuu")
    var linssplit = lines.flatMap((s: String) => s.split(" "))
    println("rest1 :" + linssplit)
//rest1 :List(atguiug, han, hello, atguigu, han, aaa, ccc, bbb, aaa, ddd, uuu)

    //  2  扁平化之后 作为map进行记录次数
    //  var res2 = linssp lit.map((s:String) =>(s,1))
    var res2: Seq[(String, Int)] = linssplit.map((_, 1))
    println("rest2 :" + res2)
//rest2 :List((atguiug,1), (han,1), (hello,1), (atguigu,1), (han,1), (aaa,1), (ccc,1), (bbb,1), (aaa,1), (ddd,1), (uuu,1))
    //3  进行分钟 把不同的单词归属到不同的租(以前没有说明)
    // res2.groupBy((x:(String,Int)) => x._1)
    var res3 = res2.groupBy(_._1)
    println("res3 " + res3)
    //res3 Map(han -> List((han,1), (han,1)), ddd -> List((ddd,1)), ccc -> List((ccc,1)), uuu -> List((uuu,1)), atguigu -> List((atguigu,1)), bbb -> List((bbb,1)), atguiug -> List((atguiug,1)), hello -> List((hello,1)), aaa -> List((aaa,1), (aaa,1)))
   // val res5 = res3.map((x: (String, List[(String, Int)])) => (x._1, x._2.size))
    var res4 = res3.map((x) => (x._1, x._2.size))
    println("res4  " + res4)
    //res4  Map(han -> 2, ddd -> 1, ccc -> 1, uuu -> 1, atguigu -> 1, bbb -> 1, atguiug -> 1, hello -> 1, aaa -> 2)
    //4　对各个元组进行统计大小
    var res5 = res4.toList.sortBy((x:(String,Int))=> x._2).reverse
    println("res5  " + res5)
//res5  List((aaa,2), (han,2), (hello,1), (atguiug,1), (bbb,1), (atguigu,1), (uuu,1), (ccc,1), (ddd,1))

  }
}